package com.jml.zlhq.app.adapter;

import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CommentAdapter extends IBaseAdapter {
	private List<JSONObject> itemList;
	private AppContext appContext;
	@SuppressWarnings("unused")
	private Activity activity;
	private boolean star;

	public CommentAdapter(Activity activity, List<JSONObject> itemList, boolean star) {
		this.itemList = itemList;
		this.appContext = (AppContext) activity.getApplication();
		this.activity = activity;
		this.star = star;
	}

	@Override
	public int getCount() {
		int count = itemList == null ? 0 : itemList.size();
		return count;
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		convertView = createItem(position, convertView, parent);
		return convertView;
	}
	
	static class ItemHolder {
		public TextView name,data,content;
		public ImageView image;
		public RatingBar ratingbar;
	}
	private View createItem(int position, View convertView, final ViewGroup parent){
		ItemHolder holder = null;
		if(convertView==null){
			holder = new ItemHolder();
			int main_faxian_item = R.layout.comment_item;
			convertView = LayoutInflater.from(appContext).inflate(main_faxian_item, null);
			holder.image = (RoundImageView) convertView.findViewById(R.id.comment_item_icon);
			holder.name = (TextView) convertView.findViewById(R.id.comment_item_name);
			holder.data = (TextView) convertView.findViewById(R.id.comment_item_data);
			holder.content = (TextView) convertView.findViewById(R.id.comment_item_content);
			holder.ratingbar = (RatingBar) convertView.findViewById(R.id.comment_item_ratingbar);
			convertView.setTag(holder);
		}else{
			holder = (ItemHolder) convertView.getTag();
		}
		JSONObject json = itemList.get(position);
		try {
			holder.name.setText(BaseTools.getFrom("user.name", json));
			ImageLoader.getInstance().displayImage(BaseTools.getFrom("user.logo_url", json), holder.image, UIUtil.getImageLoaderDisplayOptions(R.drawable.default140));
			holder.data.setText(BaseTools.getFrom("content.create_at", json));
			holder.content.setText(BaseTools.getFrom("content.text", json));
			if(star){
				holder.ratingbar.setVisibility(View.VISIBLE);
				holder.ratingbar.setRating(StringUtil.isEmpty(BaseTools.getFrom("content.score", json))?0:Float.parseFloat(BaseTools.getFrom("content.score", json)));
			}else{
				holder.ratingbar.setVisibility(View.GONE);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}
}
